package ru.cybertek.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

import java.util.List;
import java.util.Date;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;

import ru.cybertek.financial.document.PaymentOrder;
import ru.cybertek.financial.document.dao.PaymentOrderPayerDao;
import ru.cybertek.domain.Payer;
import ru.cybertek.domain.PayerImpl;
import ru.cybertek.domain.Account;
import ru.cybertek.dao.AccountDao;


public class PaymentOrderManagementAction extends ActionSupport  
{
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
	
	private String id;
	
	private String amount;
	
	private String amountInWords;
	
	private String paymentDate = dateFormat.format(new Date());
	
	private String number;
	
	private String paymentPurpose;
	
	private PaymentOrderPayerDao paymentOrderDao;
	
	private int payerId;
	
	private int payeeId;
	
	private AccountDao accountDao;
	
	
	public String list()
	{
		return SUCCESS;
	}
	
	public String delete()
	{
		paymentOrderDao.deletePaymentOrder(Integer.valueOf(id));
		
		clearFieldValue();
		
		return SUCCESS;
	}
	
	public String edit()
	{
		PaymentOrder paymentOrder = paymentOrderDao.getPaymentOrder(Integer.valueOf(id));
		
		
		id = paymentOrder.getId().toString();
		amount = paymentOrder.getAmount().toString();
		amountInWords = paymentOrder.getAmountInWords();
		number = paymentOrder.getNumber();
		paymentPurpose = paymentOrder.getPaymentPurpose();
		paymentDate = dateFormat.format(paymentOrder.getDate());
		payerId = paymentOrder.getPayer().getAccountId();
		payeeId = paymentOrder.getPayee().getAccountId();
		
		return SUCCESS;
	}
	
	public String add() throws Exception
	{
		PaymentOrder paymentOrder = new PaymentOrder();
		if ((id != null) & !id.equals("")) 
			paymentOrder.setId(Integer.valueOf(id));
		
		paymentOrder.setDate(dateFormat.parse(paymentDate));
		
		paymentOrder.setAmount(BigDecimal.valueOf(Double.valueOf(amount)));
		paymentOrder.setAmountInWords(amountInWords);
		paymentOrder.setNumber(number);
		paymentOrder.setPaymentPurpose(paymentPurpose);
		
		Account payerAccount = new Account(payerId);
		paymentOrder.setPayer(new PayerImpl(payerAccount));
		Account payeeAccount = new Account(payeeId);
		paymentOrder.setPayee(new PayerImpl(payeeAccount));
		
		paymentOrderDao.addPaymentOrder(paymentOrder);
		
		clearFieldValue();
		
		return SUCCESS;
	}
	
	public String cancel()
	{
		clearFieldValue();
		
		return SUCCESS;
	}
	
	
	private void clearFieldValue()
	{
		id = null;
		amount = null;
		amountInWords = null;
		paymentDate = dateFormat.format(new Date());
		number = null;
		paymentPurpose = null;
		payerId = 0;
		payeeId = 0;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAmount() {
		return amount;
	}

	public void setAmount(String amount) {
		this.amount = amount;
	}

	public String getAmountInWords() {
		return amountInWords;
	}

	public void setAmountInWords(String amountInWords) {
		this.amountInWords = amountInWords;
	}

	public String getPaymentDate() {
		return paymentDate;
	}

	public void setPaymentDate(String paymentDate) {
		this.paymentDate = paymentDate;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getPaymentPurpose() {
		return paymentPurpose;
	}

	public void setPaymentPurpose(String paymentPurpose) {
		this.paymentPurpose = paymentPurpose;
	}

	public PaymentOrderPayerDao getPaymentOrderDao() {
		return paymentOrderDao;
	}

	public void setPaymentOrderDao(PaymentOrderPayerDao payerOrderDao) {
		this.paymentOrderDao = payerOrderDao;
	}

	public int getPayerId() {
		return payerId;
	}

	public void setPayerId(int payerId) {
		this.payerId = payerId;
	}

	public int getPayeeId() {
		return payeeId;
	}

	public void setPayeeId(int payeeId) {
		this.payeeId = payeeId;
	}

	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}


}
